home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr48 / 386p_200.zip / PATTERN.ASM < prev    next >
Assembly Source File  |  1995-01-12  |  15KB  |  641 lines

  1. ; Tiled background  management routines
  2. ; A "tiled" background is a background image made of rectangular bitmaps
  3. ; This module uses square shaped bitmaps, due to the x/y aspect ratio
  4. ; they are 32x26 pixel wide because of the different aspect ratio
  5. ; ( 4/3 for the monitor, 320/200 for the pixel, and another one for
  6. ;   the maximum image on the monitor)
  7. ;
  8. ; I'm used to call "patterns" these 32x26 tiles, the reason is that i'm 
  9. ; a native italian that started designing games at the high school 
  10. ; and never heard the word "tiled background" before reading 
  11. ; it on rec.games.programmer.
  12. ; So, when i reinvented the wheel i had to give it a name :] .
  13.  
  14.        .386P
  15. code32 segment para public use32
  16.        assume cs:code32,ds:code32
  17. NOSMART
  18. NOJUMPS
  19. include 386video.inc
  20. include chtil.inc
  21.  
  22. PXTILE  = (TWIDTH)
  23. MAPITEM = 8        
  24. HALFMAPITEM = (MAPITEM/2)
  25. HALFXTILE = (PXTILE/2)
  26.  
  27. PATSKID dd 0
  28.  
  29. ; _GetPat
  30. ; _PutPat
  31.  
  32. ; 1 nudget     = 4  pixel (plane aligned, one pixel for every plane)
  33. ; 1 big nudget = 16 pixel (plane aligned, four nudgets)
  34.  
  35. ; PAT format:
  36. ; 2x26 dwords for each 4 planes.
  37. ; Given a plane, the 52 dwords have to be blitted as follows:
  38. ;         0  1
  39. ;         1  2
  40. ;         3  4
  41. ;        ..  ..
  42. ;        48  49
  43. ;        50  51
  44. ; As you can see a "pattern" is just a "picture" with size 32x26
  45. ; with optimized routines to blit it
  46. ; N.B. The standard name for files of patterns is *.PTF
  47.  
  48.         public _GetPat
  49.  
  50. ; reads pattern from screen
  51. _GetPat:
  52.         ; in:
  53.         ; edi = pic dest
  54.         ; esi = scr source base
  55.         ; eax = x in pixels
  56.         ; edx = y in pixels
  57.         ; out:
  58.         ; edi = ptr to end of picture
  59.         pushad
  60.         mov ebp,_ScrX
  61.         add esi,eax
  62.         add esi,[edx*4+_RowStart]
  63.         mov ecx,THEIGHT
  64.         sub ebp,TWIDTH
  65. @rowdown:        
  66.         movsd
  67.         movsd
  68.         movsd
  69.         movsd
  70.         movsd
  71.         movsd
  72.         movsd
  73.         movsd
  74.         add esi,ebp
  75.         dec ecx
  76.         jne @rowdown
  77.         
  78.         popad
  79.         add edi,TILESIZE
  80.         ; edi = posizione DOPO la lettura del pattern
  81.     ret
  82.  
  83.  
  84. patboost macro 
  85.         movsd
  86.         movsd
  87.         movsd
  88.         movsd
  89.         movsd
  90.         movsd
  91.         movsd
  92.         movsd
  93.         add edi,ebp
  94.         endm
  95.         
  96.  
  97. ; Writes a pattern to screen using an unrolled loop
  98. ; (don't worry for the movsd, the cpu-memory interface
  99. ; is stressed to the limit with this and we need "small" opcodes
  100. ; to keep the pipeline filled on 386s
  101. ; The main speed gain is obtained avoiding the loop
  102. ; and thus keeping the pipeline filled even on 386s
  103.  
  104.         public _PutPat
  105. _PutPat:
  106.         ; in:
  107.         ; edi = scr dest
  108.         ; esi = pat source 
  109.         ; eax = x in nudgets
  110.         ; edx = y in pixels
  111.         ; out:
  112.         pushad
  113.         mov ebp,RXWIDTH+XBORDER
  114.         add edi,eax
  115.         add edi,[edx*4+_RowStart]
  116.         sub ebp,TWIDTH
  117. @punup:   
  118.         patboost ;0
  119.         patboost ;1
  120.         patboost ;2
  121.         patboost ;3
  122.         patboost ;4
  123.         patboost ;5
  124.         patboost ;6
  125.         patboost ;7
  126.         patboost ;8
  127.         patboost ;9
  128.         patboost ;10
  129.         patboost ;11
  130.         patboost ;12
  131.         patboost ;13
  132.         patboost ;14
  133.         patboost ;15
  134.         patboost ;16
  135.         patboost ;17
  136.         patboost ;18
  137.         patboost ;19
  138.         patboost ;20
  139.         patboost ;21
  140.         patboost ;22
  141.         patboost ;23
  142.         patboost ;24
  143.         movsd                    ; 25
  144.         movsd                    ;
  145.         movsd
  146.         movsd
  147.         movsd
  148.         movsd
  149.         movsd
  150.         movsd
  151.         popad
  152.         ret
  153.      
  154. ; Put "Halved" pattern to screen
  155. ; Useful to show tiles on the "scoreboard" or to display a "zoom-out" map
  156.    
  157. takka macro
  158.         lodsd
  159.         shl ax,8
  160.         shr eax,8
  161.         stosw
  162.         endm
  163.         
  164. hboost macro 
  165.         takka
  166.         takka
  167.         takka
  168.         takka
  169.         takka
  170.         takka
  171.         takka
  172.         takka
  173.         add edi,ebp
  174.         add esi,TWIDTH
  175.         endm
  176.         
  177.         public _PutHPat
  178. _PutHPat:
  179.         ; in:
  180.         ; edi = scr dest
  181.         ; esi = pat source 
  182.         ; eax = x in nudgets
  183.         ; edx = y in pixels
  184.         ; out:
  185.         pushad
  186.         mov ebp,RXWIDTH+XBORDER
  187.         add edi,eax
  188.         add edi,[edx*4+_RowStart]
  189.         sub ebp,(TWIDTH/2)
  190. @hpunup:   
  191.         hboost ;0
  192.         hboost ;1
  193.         hboost ;2
  194.         hboost ;3
  195.         hboost ;4
  196.         hboost ;5
  197.         hboost ;6
  198.         hboost ;7
  199.         hboost ;8
  200.         hboost ;9
  201.         hboost ;10
  202.         hboost ;11
  203.         hboost ;13
  204.         popad
  205.         ret
  206.         
  207.  
  208. UPLEFT_TIL macro
  209.         mov ebp,xrock
  210.         mov edx,TWIDTH
  211.         mov eax,THEIGHT
  212.         mov edi,yroll        
  213.         sub edx,ebp  ; TWIDTH-xrock
  214.         sub eax,edi  ; THEIGHT-yroll
  215.         mov esi,ebp  ; xrock
  216.         mov ebx,stilt ; map start
  217.         shl edi,5   ; tile line offset
  218.         add esi,[ebx] ; tilebase+xrock+yroll*twidth
  219.         add esi,edi   ;
  220.         
  221.         mov edi,PATSKID
  222. ulzig:  mov ecx,edx ; blit TWIDTH-xrock dots
  223.         rep movsb
  224.         add edi,RXWIDTH+XBORDER
  225.         add esi,ebp
  226.         sub edi,edx
  227.         dec eax
  228.         jne ulzig      
  229.         
  230.         endm
  231.         
  232. LEFT_TIL macro
  233.         mov ebp,xrock
  234.         mov edx,TWIDTH
  235.         mov eax,THEIGHT
  236.         sub edx,ebp  ; TWIDTH-xrock
  237.         sub eax,yroll
  238.         mov esi,ebp
  239.         mov ebx,stilt
  240.         
  241.         mov edi,PATSKID
  242.         mov crick,7
  243.         cmp eax,THEIGHT
  244.         je lcrick
  245.         add edi,[eax*4+_RowStart]
  246.         add ebx,XTILES*MAPITEM
  247.         dec crick
  248. lcrick:        
  249.         mov esi,[ebx]
  250.         mov eax,THEIGHT
  251.         add ebx,XTILES*MAPITEM
  252.         add esi,ebp
  253. lzig:   
  254.         mov ecx,edx
  255.         rep movsb
  256.         add edi,RXWIDTH+XBORDER
  257.         add esi,ebp
  258.         sub edi,edx
  259.         dec eax
  260.         jne lzig      
  261.         dec crick
  262.         jne lcrick
  263.         
  264.         endm
  265.         
  266. DNLEFT_TIL macro
  267.         mov ebp,xrock
  268.         mov edx,TWIDTH
  269.         mov ecx,THEIGHT
  270.         mov eax,yroll        
  271.         sub ecx,eax
  272.         sub edx,ebp     ; TWIDTH-xrock
  273.         mov edi,[ecx*4+_RowStart]    ; 
  274.         mov esi,ebp
  275.         add edi,(RXWIDTH+XBORDER)*6*THEIGHT 
  276.         ;WARNING! RXWIDTH+XBORDER MUST MATCH _ScrX !!!!!
  277.         mov ebx,stilt
  278.         add ebx,XTILES*MAPITEM*7
  279.         add esi,[ebx] ; tilebase+xrock+yroll*twidth
  280.         add edi,PATSKID
  281. dlzig:  mov ecx,edx
  282.         rep movsb
  283.         add edi,RXWIDTH+XBORDER
  284.         add esi,ebp
  285.         sub edi,edx
  286.         dec eax
  287.         jne dlzig      
  288.         endm
  289.         
  290. UPRIGHT_TIL macro
  291.         mov edi,PATSKID
  292.         mov ebp,xrock
  293.         mov edx,TWIDTH
  294.         mov eax,THEIGHT
  295.         mov ecx,yroll        
  296.         add edi,TWIDTH*9
  297.         sub edx,ebp  ; TWIDTH-xrock
  298.         mov ebx,MAPITEM*10
  299.         add edi,edx
  300.         sub eax,ecx
  301.         add ebx,stilt
  302.         shl ecx,5
  303.         mov esi,[ebx] ; tilebase+xrock+yroll*twidth
  304.         add esi,ecx   ;
  305. urzig:  mov ecx,ebp
  306.         rep movsb
  307.         add edi,RXWIDTH+XBORDER
  308.         add esi,edx
  309.         sub edi,ebp
  310.         dec eax
  311.         jne urzig      
  312.         endm
  313.         
  314. RIGHT_TIL macro
  315.         mov ebp,xrock
  316.         mov edx,TWIDTH
  317.         mov edi,PATSKID
  318.         mov eax,THEIGHT
  319.         sub edx,ebp  ; TWIDTH-xrock
  320.         sub eax,yroll
  321.         mov esi,ebp
  322.         mov ebx,stilt
  323.         add edi,edx
  324.         add ebx,(MAPITEM*10)
  325.         mov crick,7
  326.         add edi,TWIDTH*9
  327.         cmp eax,THEIGHT
  328.         je rcrick
  329.         add edi,[eax*4+_RowStart]
  330.         add ebx,(XTILES*MAPITEM)
  331.         dec crick
  332. rcrick:        
  333.         mov esi,[ebx]
  334.         mov eax,THEIGHT
  335.         add ebx,XTILES*MAPITEM
  336. rzig:   
  337.         mov ecx,ebp
  338.         rep movsb
  339.         add edi,RXWIDTH+XBORDER
  340.         add esi,edx
  341.         sub edi,ebp
  342.         dec eax
  343.         jne rzig      
  344.         dec crick
  345.         jne rcrick
  346.  
  347.         endm  
  348.         
  349. DNRIGHT_TIL macro
  350.         mov edi,PATSKID
  351.         mov ebp,xrock
  352.         mov edx,TWIDTH
  353.         mov eax,yroll        
  354.         add edi,TWIDTH*9
  355.         sub edx,ebp  ; TWIDTH-xrock
  356.         mov ebx,(MAPITEM*10)+(XTILES*MAPITEM*7)
  357.         add edi,edx
  358.         mov ecx,THEIGHT
  359.         add ebx,stilt
  360.         sub ecx,eax
  361.         add edi,(RXWIDTH+XBORDER)*THEIGHT*6 
  362.         ; WARNING! RXWIDTH+XBORDER MUST MATCH _ScrX
  363.         mov esi,[ebx] ; tilebase+xrock+yroll*twidth
  364.         add edi,[ecx*4+_RowStart]
  365. drzig:  mov ecx,ebp
  366.         rep movsb
  367.         add edi,RXWIDTH+XBORDER
  368.         add esi,edx
  369.         sub edi,ebp
  370.         dec eax
  371.         jne drzig      
  372.         endm
  373.         
  374. muvup macro
  375.         movsd
  376.         movsd
  377.         movsd
  378.         movsd
  379.         movsd
  380.         movsd
  381.         movsd
  382.         movsd
  383.         add edi,((RXWIDTH+XBORDER)-TWIDTH)
  384.      endm
  385.     
  386. DNSTRIPE_TIL macro
  387.         mov edx,yroll
  388.         mov ebp,THEIGHT
  389.         mov edi,PATSKID
  390.         sub ebp,edx
  391.         add ebp,THEIGHT*6
  392.         mov ecx,xrock
  393.         mov ebx,stilt
  394.         mov eax,10
  395.         add edi,[ebp*4+ _RowStart]
  396.         cmp ecx,0
  397.         je dgumba
  398.         add ebx,MAPITEM
  399.         add edi,TWIDTH
  400.         dec eax
  401. dgumba:
  402.         sub edi,ecx
  403.         add ebx,XTILES*MAPITEM*7
  404. dszip:        
  405.         mov esi,[ebx]
  406.         add ebx,MAPITEM
  407.         push edi
  408.         mov ecx,edx
  409. dzip:       
  410.         movsd
  411.         movsd
  412.         movsd
  413.         movsd
  414.         movsd
  415.         movsd
  416.         movsd
  417.         movsd
  418.         add edi,((RXWIDTH+XBORDER)-TWIDTH)
  419.         dec ecx
  420.         jne dzip
  421.         
  422.         pop edi
  423.         add edi,32
  424.         dec eax
  425.         jne dszip
  426.         endm
  427.             
  428. UPSTRIPE_TIL macro                
  429.         mov edi,PATSKID
  430.         mov edx,THEIGHT
  431.         mov ebp,yroll
  432.         mov ecx,xrock
  433.         mov ebx,stilt
  434.         mov eax,10
  435.         cmp ecx,0
  436.         je gumba
  437.         add ebx,MAPITEM
  438.         add edi,TWIDTH
  439.         dec eax
  440. gumba:
  441.         sub edx,ebp
  442.         sub edi,ecx
  443.         shl ebp,5 ;line ofs
  444. uszip:        
  445.         mov esi,[ebx]
  446.         add ebx,MAPITEM
  447.         add esi,ebp
  448.         push edi
  449.         mov ecx,edx
  450. uzip:       
  451.         movsd
  452.         movsd
  453.         movsd
  454.         movsd
  455.         movsd
  456.         movsd
  457.         movsd
  458.         movsd
  459.         add edi,((RXWIDTH+XBORDER)-TWIDTH)
  460.         dec ecx
  461.         jne uzip
  462.         
  463.         pop edi
  464.         add edi,32
  465.         dec eax
  466.         jne uszip
  467.         endm
  468.         
  469. FULL_TIL macro                
  470.         mov edi,PATSKID
  471.         mov ecx,THEIGHT
  472.         mov ebp,yroll
  473.         mov edx,10 ; max complete tile columns on screen
  474.         sub ecx,ebp
  475.         mov eax,xrock
  476.         mov ebx,stilt
  477.         cmp eax,0         ; test xrock
  478.         je fgumba         ;
  479.         add edi,TWIDTH    ;
  480.         dec edx           ;
  481.         add ebx,MAPITEM   ;
  482. fgumba:
  483.         sub edi,eax ; xrock
  484.         
  485.         mov eax,7 ; max complete tile rows
  486.         cmp ebp,0                   ; test yroll
  487.         je fuszip                   ;
  488.         dec eax                     ;
  489.         add edi,[ecx*4+ _RowStart]  ;
  490.         add ebx,XTILES*MAPITEM      ; 
  491. fuszip: push ebx       
  492.         push edi
  493.         mov ecx,edx ; line in tiles
  494. fuzip:  mov esi,[ebx]
  495.         add ebx,MAPITEM 
  496.         muvup ;1
  497.         muvup ;2
  498.         muvup ;3
  499.         muvup ;4
  500.         muvup ;5
  501.         muvup ;6
  502.         muvup ;7
  503.         muvup ;8
  504.         muvup ;9
  505.         muvup ;0
  506.         muvup ;1
  507.         muvup ;2
  508.         muvup ;3
  509.         muvup ;4
  510.         muvup ;5
  511.         muvup ;6
  512.         muvup ;7
  513.         muvup ;8
  514.         muvup ;9
  515.         muvup ;0
  516.         muvup ;1
  517.         muvup ;2
  518.         muvup ;3
  519.         muvup ;4
  520.         muvup ;5
  521.               
  522.         movsd
  523.         movsd
  524.         movsd
  525.         movsd
  526.         movsd
  527.         movsd
  528.         movsd
  529.         movsd
  530.         sub edi,((RXWIDTH+XBORDER)*(THEIGHT-1))
  531.         dec ecx
  532.         jne fuzip
  533.         
  534.         pop edi
  535.         pop ebx
  536.         add edi,(RXWIDTH+XBORDER)*THEIGHT
  537.         add ebx,XTILES*MAPITEM
  538.         dec eax
  539.         jne fuszip
  540.         endm
  541.         
  542.  
  543. ; TILE MAP BLITTER
  544. ; a tile map is made of 64bit words 
  545. ; it is dimensioned as MAP[YTILES][XTILES]
  546. ; the first dword into a 64bit word is a Code32 relative offset
  547. ; pointing to the tile bitmap (pattern)
  548. ; while the upper word contains various info
  549. ; (flags, bitfield or a near pointer to an extra tile descriptor)
  550. ; the _PutMap routine just look into the lower dword.
  551. ; WARNING!
  552. ;       This code is highly optimized with instruction flow interleaving
  553. ;       and "look ahead" optimizations, it runs fast on 386s and takes
  554. ;       advantage of superior capabilities of successive intel CPUs.
  555. ;       This of course makes it harder to debug or optimize further.
  556.         
  557.         align dword
  558. xrock dd 0
  559. yroll dd 0  ; how many lines we rolled down ?
  560. stilt dd 0  ; map "hook"      
  561. crick dd 0  ; aux counter
  562.  
  563.         align byte
  564.         
  565.         public _PutMap
  566.         
  567. _PutMap:; Tiled Background blitter optimized for CPL3    
  568.         ; ( the "optimization" consist in reducing to 4 the
  569.         ;   plane-switch instructions needed for a full background refresh)
  570.         ; esi= map file
  571.         ; eax = x in pixels  (0..VXWIDTH)
  572.         ; edx = y in pixels  (0..VYHEIGHT)
  573.         ; n.b. after blitting the map you have to set _DisplayStart at x,y
  574.         ;      & flip to this page if you wanna make it visible
  575.         
  576.         pushad
  577.         mov edi,YBORDER
  578.         mov ecx,XBORDER
  579.         add ecx,_ScrBase
  580.         add ecx,[edi*4+_RowStart]
  581.         mov PATSKID,ecx
  582.         
  583.         mov ebp,eax ; save x
  584.         
  585.         and eax,31     ; xrock
  586.         
  587.         shr ebp,5 ; pixels to tiles
  588.         
  589.         mov xrock,eax  ;
  590.         
  591.         mov eax,edx        ; yroll & ytil
  592.         mov ecx,THEIGHT    ;
  593.         mov edx,0          ;
  594.         div ecx            ;
  595.         ; edx = y mod THEIGHT
  596.         ; eax = y div THEIGHT == ytil == first visible "tile line" on map
  597.         mov yroll,edx
  598.         ; now precalc values for blitting
  599.         mov edx,(XTILES*MAPITEM)
  600.         lea esi,[esi+ebp*MAPITEM] ; start into tile row
  601.         mul edx
  602.         ; eax <-- eax*XTILES ==  ytil*XTILES == line offset into map
  603.         ; REMEMBER:
  604.         ; A pattern is 32 pix wide ==> 8 nudgets
  605.         ; there are 16 "extra" pixels on every display page
  606.         ; and a map item uses 8 byte.
  607.         ; NOW look at the following code and guess what's happening ...
  608.         ; as you can see i eliminated wasteful cmp&jmp with clever bit handling
  609.         add esi,eax ; add line offset into map
  610.         
  611.         mov stilt,esi  ; store map "hook"
  612.         
  613.         cmp xrock,0
  614.         je norock
  615.         
  616.         cmp yroll,0
  617.         je noroll
  618.         UPLEFT_TIL
  619.         UPRIGHT_TIL
  620.         DNLEFT_TIL
  621.         DNRIGHT_TIL
  622. noroll:        
  623.         LEFT_TIL
  624.         RIGHT_TIL
  625.         
  626. norock:        
  627.         cmp yroll,0
  628.         je onlyfull
  629.         UPSTRIPE_TIL 
  630.         DNSTRIPE_TIL
  631. onlyfull:        
  632.         FULL_TIL     
  633.  
  634.         popad
  635.         ret
  636.  
  637. code32 ends
  638.  
  639.  END
  640.